package org.apache.tapestry5.corelib.components;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.services.ClientBehaviorSupport;
import org.apache.tapestry5.ioc.annotations.Inject;

public class ModifiedGrid extends Grid {

	@Parameter
	private boolean inPlace;

	/**
	 * The name of the psuedo-zone that encloses the Grid.
	 */
	private String zone;

	private boolean didRenderZoneDiv;

	@Inject
	private ComponentResources resources;

	@Environmental
	private ClientBehaviorSupport clientBehaviorSupport;

    @Environmental
    private RenderSupport renderSupport;
    
    
	Object beginRender(MarkupWriter writer) {
		
		if (inPlace && zone == null) {
			
			zone = renderSupport.allocateClientId(resources);

			writer.element("div", "id", zone);

			clientBehaviorSupport.addZone(zone, null, "show");

			didRenderZoneDiv = true;
		}

		return null;
	}
	void afterRender(MarkupWriter writer) {
		if (didRenderZoneDiv) {
			writer.end(); // div
			didRenderZoneDiv = false;
		}
	}
}
